#!/usr/bin/python2.5
from googbus import googuser
from googbus import googdefs
import osso
import gtk
import dbus
import datetime
import time

def test_google_user():
    print "Testing Google user"
    print "---------------------------------------"
    
    user = googuser.GoogleUser()
    if user.read_user_data():
        print "Username: " + user.username
        print "Password: " + user.password
    else:
        print "Error while getting user data"
    
    
def test_dbus_methods():
    """
    Testing DBUS interfaces
    """
    bus = dbus.SessionBus ()
    
    print "Testing DBUS interface - Test interface"
    print "---------------------------------------"
    
    test_object = bus.get_object ( googdefs.GOOGBUS_DBUS_SERVICE , 
                                   googdefs.GOOGBUS_DBUS_TEST_OBJECT )
    test_iface = dbus.Interface(test_object, googdefs.GOOGBUS_DBUS_TEST_INTERFACE)
    
    test_result = test_iface.GetTestTuple()
    print "GetTestTuple returned: " + str(test_result) + "\n"
    
    
    print "Testing DBUS interface - Calendar"
    print "---------------------------------------"
    
    calendar_object = bus.get_object ( googdefs.GOOGBUS_DBUS_SERVICE , 
                                       googdefs.GOOGBUS_DBUS_CALENDAR_OBJECT )
    calendar_iface = dbus.Interface(calendar_object, googdefs.GOOGBUS_DBUS_CALENDAR_INTERFACE)
    
    """
    Login
    """
    login_ret = calendar_iface.Login()
    print "Login returned: " + str(login_ret)
    
    
    """
    Add event for today, duration 1 hour
    """
    start_time = time.strftime('%Y-%m-%dT%H:%M:%S.000Z', time.gmtime())
    end_time = time.strftime('%Y-%m-%dT%H:%M:%S.000Z', 
                             time.gmtime(time.time() + 3600))
    event_ret = calendar_iface.InsertNewEvent("Title", "Googbus test content", "Location", 
                                              start_time, end_time)
    print "New event added, return code: %s", event_ret
    
    """
    List all user calendars
    """
    calendars = calendar_iface.ListAllCalendars()
    print "Asked for user calendar and got these:"
    for cal in calendars:
        print "\t%s" % (cal)
    
    
    """
    List all entries that server returns
    """
    entries = calendar_iface.ListEntries()
    print "Asked for entries and got these:"
    for entry in entries:
        print "\tRaw: %s" % (str(entry))
        stime = datetime.datetime.fromtimestamp(entry[1])
        etime = datetime.datetime.fromtimestamp(entry[2])
        print "\t\tName: %s, Start time: %s, End time: %s" % (entry[0], str(stime), str(etime))
        
    
    """
    List entries from today onwards
    """
    from_entries = calendar_iface.ListEntriesFromToday()
    print "Asked entries from today"
    for entry in from_entries:
        print "\tRaw: %s" % (str(entry))
        stime = datetime.datetime.fromtimestamp(entry[1])
        etime = datetime.datetime.fromtimestamp(entry[2])
        print "\t\tName: %s, Start time: %s, End time: %s" % (entry[0], str(stime), str(etime))
    
    


test_google_user()
test_dbus_methods()

